module MUX_OH #(
    parameter KW = 2,  // one hot Key Width
    parameter DW = 32,  // Data Width
    parameter CHECK_OH = 1,
    parameter DEFAULT = {DW{1'b0}}
) (
    input  [KW-1:0] sel,
    input  [DW-1:0] din [KW-1:0],
    output [DW-1:0] dout
);
  integer i;
  logic [DW-1:0] out;
  always_comb begin
    out = DEFAULT;
    for (i = 0; i < KW; i = i + 1) begin
      out = out | ({DW{sel[KW-1-i]}} & din[KW-1-i]);
    end
  end
  assign dout = out;
  int cnt, j;
  always_comb begin
    if (CHECK_OH) begin
      cnt = 0;
      for (j = 0; j < KW; j = j + 1) begin
        cnt = cnt + {31'b0, sel[j]};
      end
      if (cnt > 1) begin
        $display("%m sel 0x%x is not Ont Hot code", sel);
        $finish;
      end
    end
  end
endmodule

module MUX_PRIO #(
    parameter KW = 2,  // sel Key Width
    parameter DW = 32,  // Data Width
    parameter PRIO_HIGH = 0,  // 0 means lsb is prior, 1 means msb is prior
    parameter DEFAULT = {DW{1'b0}}
) (
    input  [KW-1:0] sel,
    input  [DW-1:0] din [KW-1:0],
    output [DW-1:0] dout
);
  integer i;
  logic [DW-1:0] out;
  always_comb begin
    out = DEFAULT;
    for (i = 0; i < KW; i = i + 1) begin
      if (PRIO_HIGH == 0) begin
        if(sel[KW-1-i]) out = din[KW-1-i];
      end else begin
        if(sel[i]) out = din[i];
      end
    end
  end
  assign dout = out;
endmodule

